#!/usr/local/bin/python3
# coding=utf-8

import click
import os
from pathlib import Path
from click.utils import open_file
import subprocess

class HandleInput:

    def __init__(self, shouldCleanPods = "y", shouldCleanPodCache = "n",shouldUpdateCode = "n",shouldUpdateRepo = "n", shouldCleanDerivedData = "n") :
        self.shouldCleanPods = shouldCleanPods
        self.shouldCleanPodCache = shouldCleanPodCache
        self.shouldUpdateCode = shouldUpdateCode
        self.shouldUpdateRepo = shouldUpdateRepo
        self.shouldCleanDerivedData = shouldCleanDerivedData
        self.isDryRun = "n"

    
    def collect_shouldCleanPods(self):
        self.shouldCleanPods = input("是否需要删除Pods文件夹和Podfile.lock (y/n):")
        if self.shouldCleanPods == "y" or self.shouldCleanPods == "n":
            return
        else:
            self.collect_shouldCleanPods()

    def collect_shouldCleanPodCache(self):
        self.shouldCleanPodCache = input("是否需要删除全部pod cache (y/n):")
        if self.shouldCleanPodCache == "y" or self.shouldCleanPodCache == "n":
            return
        else:
            self.collect_shouldCleanPodCache()

    def collect_shouldUpdateCode(self):
        self.shouldUpdateCode = input("是否需要拉取项目最新代码 (y/n):")
        if self.shouldUpdateCode == "y" or self.shouldUpdateCode == "n":
            return
        else:
            self.collect_shouldUpdateCode()

    def collect_shouldUpdateRepo(self):
        self.shouldUpdateRepo = input("是否需要更新索引库 (y/n):")
        if self.shouldUpdateRepo == "y" or self.shouldUpdateRepo == "n":
            return
        else:
            self.collect_shouldUpdateRepo()

    def collect_shouldCleanDerivedData(self):
        self.shouldCleanDerivedData = input("是否需要删除DerivedData (y/n):")
        if self.shouldCleanDerivedData == "y" or self.shouldCleanDerivedData == "n":
            return
        else:
            self.collect_shouldCleanDerivedData()


    def handle_shouldCleanPods(self):
        if self.isDryRun == "y":
            return
        if self.shouldCleanPods == "y":
            print("\r正在删除Pods文件夹和Podfile.lock")
            os.system("rm -rf ./Pods")
            os.system("rm -rf ./Podfile.lock")
            print("\r删除Pods文件夹和Podfile.lock success")

    def handle_shouldCleanPodCache(self):
        if self.isDryRun == "y":
            return
        if self.shouldCleanPodCache == "y":
            print("\r正在清理pod cache")
            status = os.system("pod cache clean --all")
            if status != 0:
                print("handle_shouldCleanPodCache error")
                exit(-1)

    def handle_shouldUpdateCode(self):
        currentBranch = subprocess.getoutput("git rev-parse --abbrev-ref HEAD")
        if self.isDryRun == "y":
            return
        if self.shouldUpdateCode == "y":
            print("\r正在更新代码")
            status = os.system("git stash")
            status = os.system("git pull origin {0} --tags".format(currentBranch))
            status = os.system("git stash pop")
            status = os.system("git branch --set-upstream-to=origin/{0} {1}".format(currentBranch,currentBranch))
            if status != 0:
                print("handle_shouldUpdateCode error")
                exit(-1)

    def handle_shouldUpdateRepo(self):
        if self.isDryRun == "y":
            return
        if self.shouldUpdateRepo == "y":
            print("\r正在更新Repo索引")
            status = os.system("PodRepoUpdate")
            if status != 0:
                print("handle_shouldUpdateRepo error")
                exit(-1)
            
    def handle_shouldCleanDerivedData(self):
        if self.isDryRun == "y":
            return
        if self.shouldCleanDerivedData == "y":
            print("\r正在删除DerivedData")
            status = os.system("rm -rf /Users/`whoami`/Library/Developer/Xcode/DerivedData")
            if status != 0:
                print("handle_shouldCleanDerivedData error")
                exit(-1)

    def handle_last_step(self):
        if self.isDryRun == "y":
            return
        cmd_str = "pod update --verbose --no-repo-update --clean-install"
        print(cmd_str)
        status = os.system(cmd_str)
        if status != 0:
            print("pod update error")
            exit(-1)
        subput = subprocess.getoutput("ls -d *.xcodeproj")
        print("Your project is:\n")
        print("{}".format(subput))
        pwd = os.getcwd()
        Pods_path = pwd + "/Pods"
        Pods = Path(Pods_path)
        
        if self.isDryRun != "y" and self.shouldCleanPods == "y" and subput == "DUApp.xcodeproj" and Pods.exists :
            os.system("rm -rf ~/.cocoapods/du-Pods >/dev/null 2>/dev/null")
        print("🐂🐂🐂🐂🐂🐂🐂🐂🐂🐂🐂🐂🐂🐂🐂🐂🐂🐂🐂🐂🐂🐂🐂🐂🐂")


@click.command()
@click.option("--mode", "-m", default="auto", show_default=True, type=click.Choice(['auto', 'manu'], case_sensitive=False), help="选择您要使用的模式")
def myPodupdate(mode):
    handle = HandleInput()
    if mode == "auto":
        pass
    else:
        handle.collect_shouldCleanPods()
        handle.collect_shouldCleanPodCache()
        handle.collect_shouldUpdateCode()
        handle.collect_shouldUpdateRepo()
        handle.collect_shouldCleanDerivedData()
    handle.handle_shouldCleanPods()
    handle.handle_shouldCleanPodCache()
    handle.handle_shouldUpdateCode()
    handle.handle_shouldUpdateRepo()
    handle.handle_shouldCleanDerivedData()
    handle.handle_last_step()

if __name__ == "__main__":
    myPodupdate()

